package com.zhengqiangblog.blog.web;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.zhengqiangblog.blog.exception.ResourceNotFoundException;
import com.zhengqiangblog.blog.model.Article;
import com.zhengqiangblog.blog.model.Category;
import com.zhengqiangblog.blog.model.Pagination;
import com.zhengqiangblog.blog.service.ArticleService;
import com.zhengqiangblog.blog.service.CategoryService;
import com.zhengqiangblog.blog.service.PaginationCalculator;
import com.zhengqiangblog.blog.service.SideBarService;
import com.zhengqiangblog.blog.web.form.PageForm;
import com.zhengqiangblog.core.ApplicationContext;
import com.zhengqiangblog.core.Model;

public class BlogAction {

	private static final Logger logger = Logger.getLogger(BlogAction.class);

	private ArticleService articleService = ApplicationContext.get(ArticleService.class);
	private CategoryService categoryService = ApplicationContext.get(CategoryService.class);
	private PaginationCalculator paginationCalculator = ApplicationContext.get(PaginationCalculator.class);
	private SideBarService sideBarService = ApplicationContext.get(SideBarService.class);

	public BlogAction() {
		logger.info("BlogAction initializing...");
	}

	public String execute(Model model, PageForm form) throws Exception {
		model.addAttribute("position", "博客");
		int totalItems = articleService.getArticleIdList().size();
		Pagination pagination = paginationCalculator.calculate(form.getPage(), totalItems);
		model.addAttribute("pagination", pagination);
		int start = (pagination.getPageNo() - 1) * pagination.getPageSize();
		List<Article> articles = articleService.getArticleList(start, pagination.getPageSize());
		model.addAttribute("articles", articles);
		sideBarService.initialize(model);
		return "BLOG";
	}

	public String archive(Model model, PageForm form) throws Exception {
		String strYear = model.getPathVariable("year");
		String strMonth = model.getPathVariable("month");
		model.addAttribute("position", "存档：" + strYear + "年" + strMonth + "月");
		int year = 0;
		int month = 0;
		if (StringUtils.isNoneBlank(strYear)) {
			year = Integer.parseInt(strYear);
		}
		if (StringUtils.isNotBlank(strMonth)) {
			month = Integer.parseInt(strMonth);
		}
		int totalItems = articleService.getArticleIdListByDate(year, month).size();
		Pagination pagination = paginationCalculator.calculate(form.getPage(), totalItems);
		model.addAttribute("pagination", pagination);
		int start = (pagination.getPageNo() - 1) * pagination.getPageSize();
		List<Article> articles = articleService.getArticleListByDate(year, month, start, pagination.getPageSize());
		model.addAttribute("articles", articles);
		sideBarService.initialize(model);
		return "BLOG";
	}

	public String tag(Model model, PageForm form) throws Exception {
		String tagName = model.getPathVariable("tagName");
		model.addAttribute("position", "标签：" + tagName);
		int totalItems = articleService.getArticleIdListByTag(tagName).size();
		Pagination pagination = paginationCalculator.calculate(form.getPage(), totalItems);
		model.addAttribute("pagination", pagination);
		int start = (pagination.getPageNo() - 1) * pagination.getPageSize();
		List<Article> articles = articleService.getArticleListByTag(tagName, start, pagination.getPageSize());
		model.addAttribute("articles", articles);
		sideBarService.initialize(model);
		return "BLOG";
	}

	public String category(Model model, PageForm form) throws Exception {
		String strCategoryId = model.getPathVariable("categoryId");
		int categoryId = 0;
		if (StringUtils.isNoneBlank(strCategoryId)) {
			categoryId = Integer.parseInt(strCategoryId);
		}
		Category category = categoryService.getCategory(categoryId);
		if (category == null) {
			throw new ResourceNotFoundException();
		}
		model.addAttribute("position", "分类：" + category.getName());
		int totalItems = articleService.getArticleIdListByCategory(categoryId).size();
		Pagination pagination = paginationCalculator.calculate(form.getPage(), totalItems);
		model.addAttribute("pagination", pagination);
		int start = (pagination.getPageNo() - 1) * pagination.getPageSize();
		List<Article> articles = articleService.getArticleListByCategory(categoryId, start, pagination.getPageSize());
		model.addAttribute("articles", articles);
		sideBarService.initialize(model);
		return "BLOG";
	}

}
